package edu.ccut.saturn.workflow.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import edu.ccut.saturn.workflow.common.Constants;

public class WorkFlowInfo    {
	private String x;
	private String y;
    private String key;
    private String name;
    private String version;
    private String author;
    private String description;
    private List<ActivityInfo> activities;
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#getKey()
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#getKey()
	 */
	public String getKey() {
		return key;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#setKey(java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#setKey(java.lang.String)
	 */
	public void setKey(String key) {
		this.key = key;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#getName()
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#getName()
	 */
	public String getName() {
		return name;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#setName(java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#setName(java.lang.String)
	 */
	public void setName(String name) {
		this.name = name;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#getVersion()
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#getVersion()
	 */
	public String getVersion() {
		return version;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#setVersion(java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#setVersion(java.lang.String)
	 */
	public void setVersion(String version) {
		this.version = version;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#getAuthor()
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#getAuthor()
	 */
	public String getAuthor() {
		return author;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#setAuthor(java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#setAuthor(java.lang.String)
	 */
	public void setAuthor(String author) {
		this.author = author;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#getDescription()
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#getDescription()
	 */
	public String getDescription() {
		return description;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#setDescription(java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#setDescription(java.lang.String)
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#getActivities()
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#getActivities()
	 */
	public List<ActivityInfo> getActivities() {
		return activities;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#setActivities(java.util.List)
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#setActivities(java.util.List)
	 */
	public void setActivities(List<ActivityInfo> activities) {
		this.activities = activities;
	}
    /* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#getActivityInfoById(java.lang.String)
	 */
    /* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#getActivityInfoById(java.lang.String)
	 */
    public ActivityInfo getActivityInfoById(String id){
    	for(ActivityInfo ai: activities){
    		if(id.equals(ai.getId())){
    			return ai;
    		}
    	}
    	return null;
    }
    /* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#getStartInfo()
	 */
    /* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#getStartInfo()
	 */
    public StartInfo getStartInfo(){
    	for(ActivityInfo ai: activities){
    		if(Constants.ACTIVITY_TYPE_START.equals(ai.getType())){
    			return (StartInfo)ai;
    		}
    	}
    	return null;
    }
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#getX()
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#getX()
	 */
	public String getX() {
		return x;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#setX(java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#setX(java.lang.String)
	 */
	public void setX(String x) {
		this.x = x;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#getY()
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#getY()
	 */
	public String getY() {
		return y;
	}
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IWorkFlowInfo#setY(java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see edu.ccut.saturn.workflow.vo.IvoWorkFlowInfo#setY(java.lang.String)
	 */
	public void setY(String y) {
		this.y = y;
	}
	/**
	 * 得到指定活动的前驱活动
	 * 
	 * @param workflowInfo
	 * @param activityId
	 * @return
	 */
	public ArrayList<ActivityInfo> getBeforeActivityByActId( String activityId) {
		ArrayList<ActivityInfo> list = new ArrayList<ActivityInfo>();
		List<ActivityInfo> allAct = getActivities();
		for (ActivityInfo act : allAct) {
			String nextAct = act.getNext();
			if (activityId.equals(nextAct)) {
				list.add(act);
			}
			if(Constants.ACTIVITY_TYPE_DECISION.equals(act.getType())){
				DecisionInfo decision = (DecisionInfo)act;
				List<IfInfo> iflist= decision.getIfinfos();
				for(IfInfo ifthen : iflist){
					if(activityId.equals(ifthen.getThen())){
						list.add(act);
					}
				}
			}
		}
		return list;
	}

	/**
	 * 得到指定活动的前驱活动 不包括分支活动
	 * 
	 * @param workflowInfo
	 * @param activityId
	 * @return
	 */
	public ArrayList<ActivityInfo> getBeforeActivityWithoutDecision(String activityId) {
		ArrayList<ActivityInfo> list = new ArrayList<ActivityInfo>();
		list = getBeforeActivityByActId(activityId);
		for(Iterator<ActivityInfo> it = list.iterator();it.hasNext();){
			if (Constants.ACTIVITY_TYPE_DECISION.equals(it.next().getType())) {
				it.remove();
			}
		}
		return list;
	}
	/**
	 * 得到指定活动的前驱分支活动
	 * 
	 * @param workflowInfo
	 * @param activityId
	 * @return
	 */
	public ArrayList<ActivityInfo> getBeforeActivityOnlyDecision(String activityId) {
		ArrayList<ActivityInfo> list = new ArrayList<ActivityInfo>();
		list = getBeforeActivityByActId(activityId);
		for(Iterator<ActivityInfo> it = list.iterator();it.hasNext();){
			if (!Constants.ACTIVITY_TYPE_DECISION.equals(it.next().getType())) {
				it.remove();
			}
		}
		return list;
	}
}
